package ohc.login.mixin;

import net.minecraft.network.packet.c2s.play.ClickSlotC2SPacket;
import net.minecraft.network.packet.c2s.play.CreativeInventoryActionC2SPacket;
import net.minecraft.network.packet.c2s.play.UpdateSelectedSlotC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import ohc.login.LoginManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

/**
 * 阻止未登录玩家操作物品栏
 */
@Mixin(ServerPlayNetworkHandler.class)
public class PlayerInventoryMixin {
    
    @Shadow
    public ServerPlayerEntity player;
    
    @Inject(method = "onClickSlot", at = @At("HEAD"), cancellable = true)
    private void onClickSlot(ClickSlotC2SPacket packet, CallbackInfo ci) {
        if (!LoginManager.INSTANCE.isLoggedIn(player.getUuid())) {
            ci.cancel();
        }
    }
    
    @Inject(method = "onCreativeInventoryAction", at = @At("HEAD"), cancellable = true)
    private void onCreativeInventoryAction(CreativeInventoryActionC2SPacket packet, CallbackInfo ci) {
        if (!LoginManager.INSTANCE.isLoggedIn(player.getUuid())) {
            ci.cancel();
        }
    }
    
    @Inject(method = "onUpdateSelectedSlot", at = @At("HEAD"), cancellable = true)
    private void onUpdateSelectedSlot(UpdateSelectedSlotC2SPacket packet, CallbackInfo ci) {
        if (!LoginManager.INSTANCE.isLoggedIn(player.getUuid())) {
            ci.cancel();
        }
    }
}
